package com.aaa.advice;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.fpa.service.LogService;
import com.aaa.pojo.Log;
import com.aaa.pojo.Users;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * @author jiaway
 * @version 1.0
 * @description:
 * @date create in 2022/9/8 16:28
 * @modified By：
 */
@Component
@Aspect
public class MyAdvice {

    @Autowired
    private LogService logService;

    @Around("execution(* com.aaa.fpa.controller.*.*(..))")
    public Object around(ProceedingJoinPoint joinPoint)  {
        // 原来需要执行的方法
/*        Object result = joinPoint.proceed();
        // 获取到方法签名对象，就可以获取到方法上的其他信息
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
       // 获取到方法对象
        Method method = signature.getMethod();

        // 请求的类名和方法名
        String className = joinPoint.getTarget().getClass().getName();
        // 获取到方法名
        String methodName = signature.getName();

        System.out.println("className:" +className +" methodName:"+ methodName);
        return result;*/
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        //保存日志信息到数据库中
        Log log = new Log();
        //获取请求的ip地址
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        log.setIp(ip.equals("0:0:0:0:0:0:0:1") ? "192.168.137.1" : ip);
        HttpSession session = request.getSession();
        //获取类名
        String className = joinPoint.getTarget().getClass().getName();
        log.setClassName(className);
        //获取方法名
        String[] split = signature.toString().split("\\.");
        String method = split[split.length - 1];
        log.setMethodName(method);
        //获取参数
        String paramString = Arrays.toString(args);
        log.setParams(paramString);
        if (paramString.getBytes().length > 8889) {
            log.setParams("Too Long Params To Save ");
        }
        //设置时间
        log.setCreateTime(new Date());
        if (ObjectUtil.isNotEmpty(session.getAttribute("users"))) {
            Users userinfo = (Users) session.getAttribute("users");
            log.setLoginName(userinfo.getUsername());
            log.setUserName(userinfo.getUsername());
            logService.addLog(log);
        }
        try {
            //业务方法执行
            Object proceed = joinPoint.proceed(args);
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }


    }

